<?php

################################################################################################################
Function chk_template($install_file)
{
	if (!file_exists($install_file))
		return (FALSE);
	require ($install_file);
	if (trim($name) == "")
		return (FALSE);
	if (trim($version) == "")
		return (FALSE);
	return (TRUE);
}

################################################################################################################
Function is_new_template($template)
{
	$table_templates = $_SESSION['prefix']."templates";
	$req = dbquery("SELECT tid FROM $table_templates WHERE path='$template'");
	if (@mysql_num_rows($req) > 0)
		 return (FALSE);
	return (TRUE);
}

################################################################################################################
Function is_default_template($tid)
{
	$table_templates = $_SESSION['prefix']."templates";
	$req = dbquery("SELECT tid FROM $table_templates WHERE `default`='ok'");
	$tid_res = @mysql_result($req, 0, "tid");
	if ($tid_res == $tid)
		return (TRUE);
	return(FALSE);
}

################################################################################################################
Function is_in_use_template($tid)
{
	$table_users_auth = $_SESSION['prefix']."users_auth";
	$req = dbquery("UPDATE $table_users_auth SET tid='1' WHERE tid='$tid'");
	return;
}

################################################################################################################
Function is_system_template($tid, $connect="ok")
{
	$table_templates = $_SESSION['prefix']."templates";
	$req = dbquery("SELECT `type` FROM $table_templates WHERE tid='$tid'", $connect);
  	if (@mysql_num_rows($req) > 0)
  	{
		$type = mysql_result($req, 0, "type");
		if ($type == "system")
			return (TRUE);
	}
	return (FALSE);
}

################################################################################################################
Function install_template($template)
{
	imports("odt_frm_php.users");
		
	/*
		-1 No rights
		-2 Old tmp
		-3 Bad conf
		-4 SQL
	*/
	
	if (!is_administrator($_SESSION['uid']))
		return (-1);
	
	if (!is_new_template($template))
		return (-2);
		
	$install_file = "../".$_SESSION['path_templates']."/".$template."/install_tmp.php";
	if (!chk_template($install_file))
		return (-3);
	
	require ($install_file);
	$table_templates = $_SESSION['prefix']."templates";
	
	$connection = dbconnect();
	$nid = create_mysql_id($table_templates, "tid", $connection);
	$req = dbquery("INSERT INTO $table_templates VALUES($nid, '$name', '$template', 'user', '$version', 'ko')", $connection);
	if (@mysql_affected_rows() == -1)
	{
		@mysql_close($connection);
		return (-4);
	}
	@rename($install_file, $install_file.".bak");
	@mysql_close($connection);
	return (0);
}

################################################################################################################
Function delete_template($tid)
{	
	
	/*
		-1 No rights
		-2 System
		-3 Default
		-4 SQL
	*/
	
	if (!is_administrator($_SESSION['uid']))
		return (-1);
	
	if (is_system_template($tid))
		return (-2);
		
	if (is_default_template($tid))
		return (-3);
	
	is_in_use_template($tid);
	
	$table_templates = $_SESSION['prefix']."templates";
	
	$connection = dbconnect();
	$req = dbquery("SELECT path FROM $table_templates WHERE tid='$tid'", $connection);
	$template = @mysql_result($req, 0, "path");
	
	$req = dbquery("DELETE FROM $table_templates WHERE tid='$tid'", $connection);
	if (@mysql_affected_rows() == -1)
	{
		@mysql_close($connection);
		return (-4);
	}
	@mysql_close($connection);
	
	$install_file = "../".$_SESSION['path_templates']."/".$template."/install_tmp.php";
	@rename($install_file.".bak", $install_file);
	return (0);
}

################################################################################################################
Function edit_template($name, $tid, $default)
{
	if (!is_administrator($_SESSION['uid']))
		return (-1);
		
	if ((is_system_template($tid)) && ($_SESSION['admin'] != "ok"))
		return (-1);
		
	if (trim($name) == "")
		return (-2);

	$table_templates = $_SESSION['prefix']."templates";
	
	$connection = dbconnect();
	
	if ($default == "ok")
	{
		$req = dbquery("UPDATE $table_templates SET `default`='ko' WHERE 1", $connection);
		$req = dbquery("UPDATE $table_templates SET `default`='ok' WHERE tid='$tid'", $connection);
	}
	
	
	$req = dbquery("UPDATE $table_templates SET name='$name' WHERE tid='$tid'", $connection);
	if (@mysql_affected_rows() == -1)
	{
		@mysql_close($connection);
		return (-1);
	}
	@mysql_close($connection);
	return (0);
}

?>